package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.weiyi.mashangzhu.Base.BaseListAdapter;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.Area;

import java.util.List;

public class LocationAdapter extends BaseListAdapter<Area> {

    private String chooseText = "";
    private int choosePosition = -1;
    private OnItemClickListener onItemClickListener;

    public LocationAdapter(Context context, List<Area> list) {
        super(context, list);
    }

    @Override
    public View initView(int position, View convertView, ViewGroup parent) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_location_list, null);
            holder = new ViewHolder();
            holder.location = (TextView) convertView.findViewById(R.id.location);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.location.setTag(position);
        String value = "";
        Area item = mList.get(position);
        if (item != null) {
            value = item.getName();
        }
        if (value.contains("全部")) {
            holder.location.setText("全部");
        } else {
            holder.location.setText(value);
        }

        if (chooseText != null && chooseText.equals(value)) {
            holder.location.setBackgroundResource(R.drawable.shape_location_gray_bg);
            holder.location.setTextColor(mContext.getResources().getColor(R.color.light_blue));
        } else {
            holder.location.setBackgroundResource(R.drawable.shape_location_white_bg);
            holder.location.setTextColor(mContext.getResources().getColor(R.color.light_black));
        }

        holder.location.setOnClickListener(onClickListener);

        return convertView;
    }

    private class ViewHolder {
        private TextView location;
    }

    /**
     * 设置选中的position,并通知刷新其它列表
     */
    public void setChoosePosition(int position) {
        if (mList != null && position < mList.size()) {
            choosePosition = position;
            chooseText = mList.get(position).getName();
            notifyDataSetChanged();
        }
    }

    /**
     * 设置选中的position,但不通知刷新
     */
    public void setChoosePositionNoNotify(int position, List<Area> list) {
        choosePosition = position;
        mList = list;
        if (mList != null && position < list.size()) {
            chooseText = list.get(position).getName();
        }
    }

    /**
     * 获取选中的position
     */
    public int getSelectedPosition() {

        if (mList != null && choosePosition < mList.size()) {
            return choosePosition;
        }
        return -1;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    /**
     * 重新定义菜单选项单击接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            choosePosition = (Integer) view.getTag();
            setChoosePosition(choosePosition);
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, choosePosition);
            }
        }
    };

}
